package org.jseries.network.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Arrays;

public class ScanNet {
    public static void main(String[] args) throws Exception {
    int[] bounds = ScanNet.rangeFromCidr("192.168.1.255/24");

    for (int i = bounds[0]; i <= bounds[1]; i++) {
        String address = InetRange.intToIp(i);
//        System.out.println("Trying Address: " + address);
        InetAddress ip = InetAddress.getByName(address);

        try {
            if (ip.isReachable(100)) { // Try for one tenth of a second
                System.out.printf("%s is reachable (%s)\n", ip.getHostAddress(), ip.getHostName());
                Process proc = Runtime.getRuntime().exec("arp " + ip.getHostAddress());
                //Writer wr = new OutputStreamWriter( tr.getOutputStream() );
                BufferedReader rd = new BufferedReader( new InputStreamReader(proc.getInputStream() ) );
                //wr.write( "hello, world\n" );
                //wr.flush();
                String s = rd.readLine();
                System.out.println( " "+s.split(" ")[3] );
            }
        } catch (Exception e) {
            System.out.println("-Error accessing " + address + " -> "+e.getMessage());
        }
    }
}

public static int[] rangeFromCidr(String cidrIp) {
    int maskStub = 1 << 31;
    String[] atoms = cidrIp.split("/");
    int mask = Integer.parseInt(atoms[1]);
    System.out.println(mask);

    int[] result = new int[2];
    result[0] = InetRange.ipToInt(atoms[0]) & (maskStub >> (mask - 1)); // lower bound
    result[1] = InetRange.ipToInt(atoms[0]); // upper bound
    System.out.println(InetRange.intToIp(result[0]));
    System.out.println(InetRange.intToIp(result[1]));

    return result;
}

public static class InetRange {
    public static int ipToInt(String ipAddress) {
        try {
            byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
            int octet1 = (bytes[0] & 0xFF) << 24;
            int octet2 = (bytes[1] & 0xFF) << 16;
            int octet3 = (bytes[2] & 0xFF) << 8;
            int octet4 = bytes[3] & 0xFF;
            int address = octet1 | octet2 | octet3 | octet4;

            return address;
        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }

    public static String intToIp(int ipAddress) {
        int octet1 = (ipAddress & 0xFF000000) >>> 24;
        int octet2 = (ipAddress & 0xFF0000) >>> 16;
        int octet3 = (ipAddress & 0xFF00) >>> 8;
        int octet4 = ipAddress & 0xFF;

        return new StringBuffer().append(octet1).append('.').append(octet2)
                                 .append('.').append(octet3).append('.')
                                 .append(octet4).toString();
    }
} }